home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / ASSEMBLE / 0572.ZIP / WHERE.C < prev   
C/C++ Source or Header  |  1987-04-23  |  6KB  |  192 lines

  1. /*
  2.  *   A simple utility for Multi-user UNIX systems
  3.  *   --------------------------------------------
  4.  *
  5.  *   Written by Steve Woodford, March 19th, 1987.
  6.  *   --------------------------------------------
  7.  *
  8.  *   This is a simple program for finding out who is on your system, where
  9.  *   in the building/s they are and whether or not they are actually at
  10.  *   their VDU's.
  11.  *
  12.  *   It takes as the standard input, the output of the who -u command, ie:
  13.  *
  14.  *    A          B              C         D      E
  15.  *
  16.  *   fred     tty003      Apr 23 14:12  0:39   10479
  17.  *
  18.  *   where :
  19.  *
  20.  *          A.  is the login name of the user.
  21.  *          B.  is the line they are on. (My system uses 3 digit nos.)
  22.  *          C.  Date and Time the user logged in. (Not used here).
  23.  *          D.  This is the time elapsed since there was any activity on
  24.  *              the line.  This could be '.' for activity within the last
  25.  *              minute, 'OLD' for no activity for 24 hours or over.
  26.  *              Otherwise it is the time in hours and minutes.
  27.  *          E.  The procces number of the user's login shell. (Not used here)
  28.  *
  29.  *   The program uses two files, /usr/lib/ttystates and /usr/lib/ttylocations.
  30.  *
  31.  *   The first contains lines of text which are to be printed for every 10
  32.  *   minutes that there has been no activity on the user's line. ie. from 1
  33.  *   to 9 minutes, the program will print the first line, from 10 to 19
  34.  *   minutes, it will print the second line and so on.  There doesn't have to
  35.  *   be as many lines as there are 10 minute segments in 24 hours, as a 
  36.  *   'compiled in' default is used in this case.
  37.  *
  38.  *   The second file is used to describe where each tty line is.  Each line
  39.  *   consists of 2 fields, the first being the name of the line - eg. tty000.
  40.  *   The second is a description of where the VDU is,
  41.  *   eg:
  42.  *       tty000 in the front room.
  43.  *
  44.  *   Two example files could be:
  45.  *
  46.  *   # cat /usr/lib/ttystates
  47.  *
  48.  *   thinking
  49.  *   getting bored
  50.  *   yawning
  51.  *   dropping off
  52.  *   drowsing
  53.  *   asleep
  54.  *   sound asleep
  55.  *   snoring gently
  56.  *   snoring loudly
  57.  *   losing consciousness
  58.  *   unconscious
  59.  *   comatose
  60.  *   dead to the world
  61.  *
  62.  *   # cat /usr/lib/ttylocations
  63.  *
  64.  *   tty000 on the console.
  65.  *   tty001 on the incoming Modem. (ie. Who knows where!!)
  66.  *   tty002 in reception.
  67.  *   tty003 in the Engineer's workshop.
  68.  *   tty004 at his desk.
  69.  *
  70.  *   So the example used above of user fred would be printed as:
  71.  *
  72.  *   fred is dropping off in the Engineer's workshop.
  73.  *
  74.  *   To avoid a lot of programming hassle with pipes etc, the program has two
  75.  *   files, a one line shell script and the actual object file.  The shell
  76.  *   script consists simply of:
  77.  *
  78.  *   who -u|/etc/where.obj
  79.  *
  80.  *   on my system I have called the shell script 'where' and the object
  81.  *   'where.obj'.  Both in /etc.  They can obviously be put anywhere in $PATH.
  82.  *
  83.  *   Finally, this program has not been made fully 'idiot-proof' and could also
  84.  *   be improved in terms of layout and efficiency. I didn't have time to
  85.  *   improve it!! (At least that's my excuse!!)
  86.  *
  87.  */
  88. #include <stdio.h>
  89.  
  90. main ()
  91. {
  92.     char c,logname[16], d[50], time[6];
  93.     char tty[8], ttytest[8], min[3];
  94.     int hour, mn, index, i;
  95.     FILE *statstrm, *ttystrm, *fopen();
  96.  
  97. /* The top of the loop for each user. */
  98.  
  99.     while (( c=getchar()) != EOF )
  100.     {
  101.         ungetc(c,stdin);
  102.  
  103. /* Ok, so this is inefficient !!!! Opening each time around the loop!! */
  104.  
  105.         if (( statstrm=fopen("/usr/lib/ttystates", "r")) == NULL )
  106.         {
  107.             fprintf(stderr,"where: can't open /usr/lib/ttystates.\n");
  108.             exit(1);
  109.         }
  110.         if (( ttystrm=fopen("/usr/lib/ttylocations", "r")) == NULL )
  111.         {
  112.             fprintf(stderr,"where: can't open /usr/lib/ttylocations.\n");
  113.             exit(1);
  114.         }
  115. /*
  116. Change this line if the output of the who -u command is different from above.
  117. The char array 'd' is used to skip over unwanted fields.
  118. */
  119.         scanf("%s %s %s %s %s %s %s",logname,tty,d,d,d,time,d);
  120.         /* Skip to start of next line. */
  121.         while ((c=getchar()) != '\n');
  122.         /* Output 'user is' */
  123.         printf("%s is",logname);
  124. /*
  125. Now split the time since activity into the three different types. First 'OLD'.
  126. Note the 'compiled in' string for OLD.
  127. */
  128.         if (strcmp(time, "OLD") == 0) printf(" decomposing");
  129. /*
  130. Seperate hours and minutes. (Ok, more inefficiency!!)
  131. And print text if valid hours and minutes.
  132. */
  133.         if ((time[2] == ':') || (time[1] == ':'))
  134.         {
  135.             hour = atoi(time);
  136.             min[0] = time[3];
  137.             if (time[1]=':') min[0] = time[2];
  138.             min[1] = '\0';
  139.             mn = atoi(min);
  140. /*
  141. Combine hours and minutes to form index into /usr/lib/ttystates.
  142. */
  143.             index = (6 * hour) + mn;
  144. /*
  145. Skip to start of line to print for this length of time.
  146. */
  147.             for (i=0; i<index; i++) while (((c=fgetc(statstrm)) != '\n') && ( c != EOF ));
  148. /*
  149. Get the next byte from the file.
  150. */
  151.             c=fgetc(statstrm);
  152.             ungetc(c,statstrm);
  153. /*
  154. Put it back and if it is EOF, print 'compiled in' default for running out of
  155. text in /usr/lib/ttylocations.
  156. */
  157.             if (c== EOF ) printf(" pushing up the daisies");
  158. /*
  159. Otherwise, print the relevant line of text.
  160. */
  161.             else
  162.             {
  163.                 putchar(' ');
  164.                 while ((c=fgetc(statstrm)) != '\n') putchar(c);
  165.             }
  166.         }
  167.         while ((c=fgetc(ttystrm)) != EOF)
  168. /*
  169. Now the loop to scan /usr/lib/ttylocations to find and print the correct entry.
  170. */
  171.         {
  172.             ungetc(c,ttystrm);
  173.             fscanf(ttystrm,"%s", ttytest);
  174. /*
  175. Get line name into ttytest and compare with line that current user is on.
  176. If they match then print the text following the line name.
  177. If not, skip the line.
  178. */
  179.             if (strcmp(tty,ttytest) == 0) while ((putchar(fgetc(ttystrm))) != '\n');
  180.             else while ((c=fgetc(ttystrm)) != '\n');
  181.         }
  182.         fclose(statstrm);
  183. /*
  184. Close the files - All this inefficiency !!
  185. */
  186.         fclose(statstrm);
  187.         fclose(ttystrm);
  188.     }
  189. }
  190.   fclose(statstrm);
  191. /*
  192. Close the files - All this ineff